function getUrlServlet()
{
  return "http://149.210.188.153/hexar/";
    
}


//TODO: Add correct error handling
function login(email, password)
{
		var urlAjax = getUrlServlet() + "utf?method=login&login="+email+"&password=" + password ;
		var header = "application/x-www-form-urlencoded;charset=utf-8";
		var type = "POST";
		$.ajax({
			url : urlAjax,
			type : type,
			dataType : 'JSON',
			contentType : header,
			encoding : "utf-8",
			error : function(data, status, jqx) {
                alert(status);
				
			},
			success : function(data, status, jqx) {
				if(data != null){
                    setUserId(data.id);
                    window.location.href = "menu.html";
                }
                else{
                    alert('Login was unsuccessful');
                }
			}
		});
    
}


function setUserId(userId)
{
    //$.cookie('userId', userId, { expires: 365 });
    intel.xdk.cache.setCookie('userId',userId,365);
}

function getUserId()
{
    //return  $.cookie('userId');
    return intel.xdk.cache.getCookie('userId');
}

function signOut()
{
    window.location.href= 'index.html';
    //return $.removeCookie('userId');
    return intel.xdk.cache.removeCookie('userId');
}

function register(name, email, password)
{
    
    var urlAjax = getUrlServlet() + "utf?method=register&name="+name+"&login=" + email+"&password=" + password;
		var header = "application/x-www-form-urlencoded;charset=utf-8";
		var type = "POST";
		$.ajax({
			url : urlAjax,
			type : type,
			dataType : 'JSON',
			contentType : header,
			encoding : "utf-8",
			error : function(data, status, jqx) {
				
				alert(status);
				
			},
			success : function(data, status, jqx) {
                
                alert('Registration complete!');
                setUserId(data.id);
                window.location.href = 'menu.html';
                
			}
		});
    
}